package com.boco.webmaster.common.taglib;

import java.util.Iterator;
import java.util.Locale;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.taglib.html.Constants;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import java.io.IOException;


public class ErrorsTag extends TagSupport {
    protected String bundle = null;

    public String getBundle() {
        return (this.bundle);
    }

    public void setBundle(String bundle) {
        this.bundle = bundle;
    }

    /**
     * The default locale on our server.
     * @deprecated Use Locale.getDefault() directly.
     */
    protected static Locale defaultLocale = Locale.getDefault();

    /**
     * The line ending string.
     */
    protected static String lineEnd = System.getProperty("line.separator");

    /**
     * The session attribute key for our locale.
     */
    protected String locale = Globals.LOCALE_KEY;

    public String getLocale() {
        return (this.locale);
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    /**
     * The message resources for this package.
     */
    protected static MessageResources messages =
        MessageResources.getMessageResources(Constants.Package + ".LocalStrings");

    /**
     * The request attribute key for our error messages (if any).
     */
    protected String name = Globals.ERROR_KEY;

    public String getName() {
        return (this.name);
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * The name of the property for which error messages should be returned,
     * or <code>null</code> to return all errors.
     */
    protected String property = null;

    public String getProperty() {
        return (this.property);
    }

    public void setProperty(String property) {
        this.property = property;
    }

    // ------------------------------------------------------- Public Methods

    /**
     * Render the specified error messages if there are any.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Were any error messages specified?
        ActionErrors errors = null;
        try {
            errors = RequestUtils.getActionErrors(pageContext, name);
        } catch (JspException e) {
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        if ((errors == null) || errors.isEmpty()) {
            return (EVAL_BODY_INCLUDE);
        }

        boolean headerPresent =
            RequestUtils.present(pageContext, bundle, locale, "errors.header");

        boolean footerPresent =
            RequestUtils.present(pageContext, bundle, locale, "errors.footer");

        boolean prefixPresent =
            RequestUtils.present(pageContext, bundle, locale, "errors.prefix");

        boolean suffixPresent =
            RequestUtils.present(pageContext, bundle, locale, "errors.suffix");

        // Render the error messages appropriately
        StringBuffer results = new StringBuffer();
        boolean headerDone = false;
        String message = null;
        Iterator reports = (property == null) ? errors.get() : errors.get(property);

        while (reports.hasNext()) {
            ActionError report = (ActionError) reports.next();
            if (!headerDone) {
                if (headerPresent) {
                    message =
                        RequestUtils.message(
                            pageContext,
                            bundle,
                            locale,
                            "errors.header");

                    results.append(message);
                    results.append("\\n");
                }
                headerDone = true;
            }

            if (prefixPresent) {
                message =
                    RequestUtils.message(
                        pageContext,
                        bundle,
                        locale,
                        "errors.prefix");
                results.append(message + "  ");
            }

            message =
                RequestUtils.message(
                    pageContext,
                    bundle,
                    locale,
                    report.getKey(),
                    report.getValues());

            if (message != null) {
                results.append(message);
            }else{
                String key = report.getKey();
                results.append(key);
            }

            if (suffixPresent) {
                message =
                    RequestUtils.message(
                        pageContext,
                        bundle,
                        locale,
                        "errors.suffix");
                results.append("  " + message);
            }

            results.append("\\n");
        }

        if (headerDone && footerPresent) {
            message =
                RequestUtils.message(pageContext, bundle, locale, "errors.footer");
            results.append(message);
            results.append(lineEnd);
        }

        try {
            if (results.length() > 0) {
                String varName = "isShowErrors";
                pageContext.getOut().write("<input type=\"hidden\" name=\"" + varName + "\" value=\"1\" />\n");
                pageContext.getOut().write("<script>\n");
                pageContext.getOut().write("function showErrors(){\n");
                pageContext.getOut().write("  actionName=\"error\";\n");
                pageContext.getOut().write("  var " + varName + " = document.forms[0].isShowErrors.value;\n");
                pageContext.getOut().write("  if(" + varName + "  == 1){\n");
                pageContext.getOut().write("    alert(\"" + results.toString() + "\");\n");
                pageContext.getOut().write("    document.forms[0]." + varName + ".value = 0;\n");
                pageContext.getOut().write("  }\n}\n\n");
                pageContext.getOut().write("</script>\n");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return (EVAL_BODY_INCLUDE);
    }

    /**
     * Release any acquired resources.
     */
    public void release() {
        super.release();
        bundle = Globals.MESSAGES_KEY;
        locale = Globals.LOCALE_KEY;
        name = Globals.ERROR_KEY;
        property = null;
    }

}
